//
//  AIHomeMainController.m
//  Mia
//
//  Created by Vill on 2024/1/11.
//

#import "AIHomeMainController.h"
#import "AIVipPlusController.h" //vip
#import "AISettingController.h" //设置
#import "AIWalletController.h"  //钱包
#import "AIHomeMainView.h"      //主页数据
#import "AIChatBoardView.h"     //keyboard
#import "AIChatTableView.h"     //聊天列表
#import "AIChatManager.h"
#import "AIPicVipAlertView.h"   //vip弹窗
#import "UIApplication+Extension.h"
#import "AIPolicyNoticeAlert.h"
#import "AIFactsController.h"   //facts

@interface AIHomeMainController ()<AIChatBoardViewDelegate>

@property (nonatomic, strong) AIHomeMainView *homeMainView;
@property (nonatomic, strong) AIChatBoardView *chatBoardView;
@property (nonatomic, strong) AIChatTableView *chatView;  // 上边的View
@property (nonatomic, assign) CGFloat toolBarY;     //高度

@end

@implementation AIHomeMainController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.toolBarY = KScreenHeight - PTW(54) - KBottomSafeHeight;
    [self.view addSubview:self.homeMainView];
    [self.homeMainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [self.view addSubview:self.chatBoardView];
//    NSLog(@" --- %ld",(long)manager.historyCount);
    // 更改背景
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"ChangeBG" UTF8String] message:[@"2" UTF8String]];
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"SelectPlayer" UTF8String] message:[@"0" UTF8String]];
    
    // 刷新金币
    [[AIUIHelperManager shared] getMyBalance];
    // 获取聊天框顶部的话题
    [[AIUIHelperManager shared] loadTopicChatTop];
    
}

#pragma mark ----------- func ----------
// 更多按钮点击
-(void)moreAction{
    // 近距离
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
    AISettingController *vc = [[AISettingController alloc] init];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    navVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.closeBlock = ^{
        [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
    };
    [self presentViewController:navVc animated:YES completion:nil];
}

// vip按钮点击
-(void)vipPlusAction{
    // 近距离
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
    AIVipPlusController *vc = [[AIVipPlusController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.closeBlock = ^{
        [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
    };
    [self presentViewController:vc animated:YES completion:nil];
}

// 钱包点击
- (void)walletAction{
    // 近距离
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
    AIWalletController *vc = [[AIWalletController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.closeBlock = ^{
        [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
    };
    [self presentViewController:vc animated:YES completion:nil];
}

// 图片点击
- (void)picAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.homeMainView.hidden = btn.selected;
}

// ar按钮点击
-(void)arBtnAction{
    [SVProgressHUD showInfoWithStatus:@"Stay tuned..."];
    [SVProgressHUD dismissWithDelay:1];
//    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"LoadARScene" UTF8String] message:[@"" UTF8String]];
}

// facts按钮点击
-(void)factsAction{
    // 近距离
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
    AIFactsController *vc = [[AIFactsController alloc] init];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    navVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.closeBlock = ^{
        [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
    };
    [self presentViewController:navVc animated:YES completion:nil];
}

// diary按钮点击
-(void)diaryAction{
    NSLog(@"diaryAction");
}

#pragma mark ----------- lazy ----------
- (AIHomeMainView *)homeMainView{
    if(!_homeMainView){
        _homeMainView = [AIHomeMainView new];
        // type  0more  1wallet  2vip  3ar  4facts  5diary
        WeakSelf(self);
        _homeMainView.homeBtnBlock = ^(NSInteger type) {
            if(type == 0){
                [weakself moreAction];
            }else if (type == 1){
                [weakself walletAction];
            }else if (type == 2){
                [weakself vipPlusAction];
            }else if (type == 3){
                [weakself arBtnAction];
            }else if (type == 4){
                [weakself factsAction];
            }else if (type == 5){
                [weakself diaryAction];
            }
        };
    }
    return _homeMainView;
}

- (AIChatBoardView *)chatBoardView{
    if (!_chatBoardView){
        _chatBoardView = [[AIChatBoardView alloc] initWithFrame:CGRectMake(0, KScreenHeight - PTW(54) - KBottomSafeHeight, KScreenWidth, PTW(54))];
        _chatBoardView.delegate = self;
    }
    return _chatBoardView;
}

-(AIChatTableView *)chatView{
    if(!_chatView){
        _chatView = [[AIChatTableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, self.toolBarY)];
        WeakSelf(self);
        _chatView.closeBlock = ^{
            weakself.homeMainView.hidden = NO;
            weakself.chatView.hidden = YES;
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
        };
    }
    return _chatView;
}

#pragma mark --------------- AIChatBoardViewDelegate 发送图片 文本 -------------------
// 输入框开始编辑
- (void)keyboardWillBeginStartEdit:(BOOL)isShow{
    if (isShow){
        [self.view addSubview:self.chatView];
        self.chatView.hidden = YES;
        [self.chatView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.chatBoardView.mas_top);
        }];
        
        self.homeMainView.hidden = YES;
        self.chatView.hidden = NO;
        // 近距离
        [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
        if ([[KIsShowNoticeAlert backNSDefaultsKey] boolValue] == false){
            //展示警告语
            AIPolicyNoticeAlert *policyalert = [[AIPolicyNoticeAlert alloc] init];
            [policyalert showInView:[[UIApplication sharedExtensionApplication] topWindow]];
        }
    }
}

// 输入框更改frame
- (void)keyboardChangeFrameWithMinY:(CGFloat)minY{
//    NSLog(@"minY ----------- %.2f",minY);
    self.toolBarY = KScreenHeight - minY;
    self.chatView.frame = CGRectMake(0, 0, KScreenWidth, self.toolBarY);
}

// 点击按钮   0相机  1问题  2语音  3视频
- (void)selectButtonWithType:(NSInteger)type{
    // 调起相机
    WeakSelf(self);
    if (type == 0){
        if ([AIUserInfoManager shareManager].userInfo.vip == false){
            //需要购买金币弹窗
            AIPicVipAlertView *vipalert = [[AIPicVipAlertView alloc] init];
            [vipalert showInView:[[UIApplication sharedExtensionApplication] topWindow]];
            // 确认充值VIP
            vipalert.certainBlock = ^{
                AIVipPlusController *vc = [[AIVipPlusController alloc] init];
                vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakself presentViewController:vc animated:YES completion:nil];
            };
        }else{
            self.homeMainView.hidden = YES;
            self.chatView.hidden = NO;
            [self chooseRictureWithResult:^(UIImage * _Nonnull chooseImage) {
                [weakself sendMessageWithImage:[NSString convertImageToBase64:chooseImage]];
            }];
        }
    }
}

// 发送图片
- (void)sendMessageWithImage:(NSString *)base64{
    AIChatModel *model = [AIChatModel new];
    model.token = [AIUserInfoManager shareManager].userInfo.token;
    model.chatType = @"SEND_PHOTO";
    AIChatContentModel *contentModel = [AIChatContentModel new];
    contentModel.photoBase64 = base64;
    model.data = contentModel;
    [[AIChatManager shareInstance] sendMessage:model timeOut:-1 tag:0];
}

// 点击发送时输入框内的文案
- (void)textViewContentText:(NSString *)textStr{
    NSLog(@" 输入的文字 -- %@",textStr);
    AIChatModel *model = [AIChatModel new];
    model.token = [AIUserInfoManager shareManager].userInfo.token;
    model.chatType = @"TEXT";
    model.chatNumber = [[NSString getCurrentDateTime:1] integerValue];
    AIChatContentModel *contentModel = [AIChatContentModel new];
    contentModel.content = textStr;
    model.data = contentModel;
    [[AIChatManager shareInstance] sendMessage:model timeOut:-1 tag:1];
}



@end
